<template>
  <div class="message-container">
    <el-card class="message-card">
      <div slot="header" class="message-header">
        <span class="header-title">我的消息</span>
      </div>
      
      <div class="message-list">
        <div v-if="messages.length === 0" class="empty-message">
          暂无消息
        </div>
        <div v-for="(message, index) in messages" 
             :key="message.id || index" 
             class="message-item">
          <div class="message-content">
            <div class="message-title">{{ message.title || '交易消息' }}</div>
            <div class="message-text">{{ message.content }}</div>
            <div class="message-time">{{ formatTime(message.createTime) }}</div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import MessageService from '@/Services/MessageService'
import { parseJwt } from '@/utils/auth'

export default {
  name: 'MessageView',
  data() {
    return {
      messages: []
    }
  },
  created() {
    // 检查用户是否登录
    if (!localStorage.getItem('isLogged')) {
      this.$router.push('/login')
      return
    }
    this.fetchMessages()
  },
  methods: {
    getCurrentUserId() {
      const token = localStorage.getItem('token')
      const decoded = parseJwt(token)
      return decoded && (
        decoded.userId ||
        decoded.id ||
        decoded.nameid ||
        decoded.sub ||
        decoded["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"]
      )
    },
    async fetchMessages() {
      const userId = this.getCurrentUserId()
      if (!userId) return
      try {
        const res = await MessageService.GetUserMessages(userId)
        if (res && res.data) {
          this.messages = Array.isArray(res.data) ? res.data : (res.data.$values || [])
          // 标记所有消息为已读
          await Promise.all(this.messages.map(msg => {
            if (!msg.isRead && msg.id) {
              return MessageService.MarkAsRead(msg.id)
            }
            return null
          }))
        }
      } catch (error) {
        this.$message.error('获取消息失败')
      }
    },
    formatTime(timeString) {
      if (!timeString) return ''
      const date = new Date(timeString)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    }
  }
}
</script>

<style scoped>
.message-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.message-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.message-list {
  min-height: 300px;
}

.message-item {
  padding: 15px;
  border-bottom: 1px solid #eee;
  transition: background-color 0.3s;
}

.message-item:hover {
  background-color: #f9f9f9;
}

.message-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.message-text {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.message-time {
  font-size: 12px;
  color: #999;
}

.empty-message {
  text-align: center;
  color: #999;
  padding: 40px 0;
  font-size: 14px;
}
</style> 